﻿namespace PlugNT.Database.Utility
{
    using IBatisNet.Common.Pagination;
    using IBatisNet.DataMapper;
    using IBatisNet.DataMapper.Configuration;
    using IBatisNet.DataMapper.Exceptions;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Reflection;

    public class BaseSqlMapDao
    {
        private ISqlMapper sqlMap;

        public BaseSqlMapDao()
        {
            Stream manifestResourceStream = Assembly.Load("IBatisNetDemo").GetManifestResourceStream("IBatisNetDemo.sqlmap.config");
            this.sqlMap = new DomSqlMapBuilder().Configure(manifestResourceStream);
        }

        protected void ExecuteDelete(string statementName, object parameterObject)
        {
            try
            {
                this.sqlMap.Delete(statementName, parameterObject);
            }
            catch (Exception exception)
            {
                throw new DataMapperException("Error executing query '" + statementName + "' for delete.  Cause: " + exception.Message, exception);
            }
        }

        protected void ExecuteInsert(string statementName, object parameterObject)
        {
            try
            {
                this.sqlMap.Insert(statementName, parameterObject);
            }
            catch (Exception exception)
            {
                throw new DataMapperException("Error executing query '" + statementName + "' for insert.  Cause: " + exception.Message, exception);
            }
        }

        protected IList<T> ExecuteQueryForList<T>(string statementName, object parameterObject)
        {
            IList<T> list;
            try
            {
                list = this.sqlMap.QueryForList<T>(statementName, parameterObject);
            }
            catch (Exception exception)
            {
                throw new DataMapperException("Error executing query '" + statementName + "' for list.  Cause: " + exception.Message, exception);
            }
            return list;
        }

        protected IList<T> ExecuteQueryForList<T>(string statementName, object parameterObject, int skipResults, int maxResults)
        {
            IList<T> list;
            try
            {
                list = this.sqlMap.QueryForList<T>(statementName, parameterObject, skipResults, maxResults);
            }
            catch (Exception exception)
            {
                throw new DataMapperException("Error executing query '" + statementName + "' for list.  Cause: " + exception.Message, exception);
            }
            return list;
        }

        protected T ExecuteQueryForObject<T>(string statementName, object parameterObject)
        {
            T local;
            try
            {
                local = this.sqlMap.QueryForObject<T>(statementName, parameterObject);
            }
            catch (Exception exception)
            {
                throw new DataMapperException("Error executing query '" + statementName + "' for object.  Cause: " + exception.Message, exception);
            }
            return local;
        }

        protected IPaginatedList ExecuteQueryForPaginatedList(string statementName, object parameterObject, int pageSize)
        {
            IPaginatedList list;
            try
            {
                list = this.sqlMap.QueryForPaginatedList(statementName, parameterObject, pageSize);
            }
            catch (Exception exception)
            {
                throw new DataMapperException("Error executing query '" + statementName + "' for paginated list.  Cause: " + exception.Message, exception);
            }
            return list;
        }

        protected void ExecuteUpdate(string statementName, object parameterObject)
        {
            try
            {
                this.sqlMap.Update(statementName, parameterObject);
            }
            catch (Exception exception)
            {
                throw new DataMapperException("Error executing query '" + statementName + "' for update.  Cause: " + exception.Message, exception);
            }
        }
    }
}

